package com.nian.common.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static final String YYYY = "yyyy";
    public static final String MM = "MM";
    public static final String DD = "dd";
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
    public static final String[] parsePatterns = { YYYY_MM_DD, YYYY_MM_DD_HH_MM_SS, YYYY_MM_DD_HH_MM,
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" };
    /**
     * 获取当前日期字符串
     * 格式: yyyy-MM-dd
     * @return
     */
	public static String getDate() {
		return getDate(YYYY_MM_DD);
	}

    /**
     * 获取当前日期
     * @return
     */
    public static Date now() {
        return new Date();
    }
    /**
     *
     * @param pattern
     * @return
     */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(now(), pattern);
	}

    /**
     * 将日期转换成指定格式的字符串
     * @param date
     * @param pattern
     * @return
     */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, YYYY_MM_DD);
		}
		return formatDate;
	}

    /**
     * yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
	public static String formatDateTime(Date date) {
		return formatDate(date, YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(now(), HH_MM_SS);
	}

    /**
     * yyyy-MM-dd HH:mm:ss
     * @return
     */
	public static String getDateTime() {
		return formatDate(now(), YYYY_MM_DD_HH_MM_SS);
	}

    /**
     * yyyy
     * @return
     */
	public static String getYear() {
		return formatDate(now(), YYYY);
	}

    /**
     * 获取当前月
     * @return
     */
	public static String getMonth() {
		return formatDate(now(), MM);
	}

    /**
     * 获取当前天
     * @return
     */
	public static String getDay() {
		return formatDate(now(), DD);
	}

	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}

    /**
     * 日期型字符串转化为日期 格式
     * ["yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" ]
     *
     * @param str
     * @return
     */
	public static Date parseDate(Object str) {
		if (str == null){
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000);
	}
	
    
	public static Date getDateStart(Date date) {
		if(date==null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		try {
			date= sdf.parse(formatDate(date, YYYY_MM_DD)+" 00:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date getDateEnd(Date date) {
		if(date==null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		try {
			date= sdf.parse(formatDate(date, YYYY_MM_DD) +" 23:59:59");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static boolean isDate(String timeString){
		SimpleDateFormat format=new SimpleDateFormat(YYYY_MM_DD);
		format.setLenient(false);
		try{
			format.parse(timeString);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	
	/**
	 * 格式化时间
	 * @param timestamp
	 * @return
	 */
	public static String dateFormat(Date timestamp){
		SimpleDateFormat format=new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		return format.format(timestamp);
	}
	
	/**
	 * 获取系统时间T imestamp
	 * @return
	 */
	public static Timestamp getSysTimestamp(){
		return new Timestamp(now().getTime());
	}
    /**生成时间随机数
     *
     * @return
     */
	public static String getDateRandom(){
		String s=new SimpleDateFormat(YYYYMMDDHHMMSSSSS).format(new Date());
		return s;
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(formatDate(parseDate("2010/3/6")));
		System.out.println(getDate("yyyy年MM月dd日 E"));
		long time = new Date().getTime()-parseDate("2012-11-19").getTime();
		System.out.println(time/(24*60*60*1000));
	}
}
